﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Collections.ObjectModel;

namespace ValidationLibrary
{
    public class ModelValidator<T>
    {
        public ReadOnlyCollection<ValidationRule<T>> Rules
        {
            get
            {
                return _rules.AsReadOnly();
            }
        }
        List<ValidationRule<T>> _rules { get; set; }

        public ModelValidator()
        {
            _rules = new List<ValidationRule<T>>();
        }

        public ValidationRule<T> Rule(Expression<Func<T, bool>> rule)
        {
            var newRule = new ValidationRule<T>(rule);
            _rules.Add(newRule);
            return newRule;
        }

        public ValidationResult<T> Validate(T validatee)
        {
            List<ValidationRuleResult<T>> results = new List<ValidationRuleResult<T>>();
            foreach (ValidationRule<T> rule in Rules)
            {
                results.Add(rule.Evaluate(validatee));
            }
            return new ValidationResult<T>(results);
        }
    }
}
